<!doctype html>
<html>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width initial-scale=1'>
    <title>设计模式-day01</title>
    <link href='https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext' rel='stylesheet' type='text/css'/>
    <style type='text/css'>html {
        overflow-x: initial !important;
    }

    :root {
        --bg-color: #ffffff;
        --text-color: #333333;
        --select-text-bg-color: #B5D6FC;
        --select-text-font-color: auto;
        --monospace: "Lucida Console", Consolas, "Courier", monospace;
    }

    html {
        font-size: 14px;
        background-color: var(--bg-color);
        color: var(--text-color);
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
    }

    body {
        margin: 0px;
        padding: 0px;
        height: auto;
        bottom: 0px;
        top: 0px;
        left: 0px;
        right: 0px;
        font-size: 1rem;
        line-height: 1.42857;
        overflow-x: hidden;
        background: inherit;
        tab-size: 4;
    }

    iframe {
        margin: auto;
    }

    a.url {
        word-break: break-all;
    }

    a:active, a:hover {
        outline: 0px;
    }

    .in-text-selection, ::selection {
        text-shadow: none;
        background: var(--select-text-bg-color);
        color: var(--select-text-font-color);
    }

    #write {
        margin: 0px auto;
        height: auto;
        width: inherit;
        word-break: normal;
        overflow-wrap: break-word;
        position: relative;
        white-space: normal;
        overflow-x: visible;
        padding-top: 40px;
    }

    #write.first-line-indent p {
        text-indent: 2em;
    }

    #write.first-line-indent li p, #write.first-line-indent p * {
        text-indent: 0px;
    }

    #write.first-line-indent li {
        margin-left: 2em;
    }

    .for-image #write {
        padding-left: 8px;
        padding-right: 8px;
    }

    body.typora-export {
        padding-left: 30px;
        padding-right: 30px;
    }

    .typora-export .footnote-line, .typora-export li, .typora-export p {
        white-space: pre-wrap;
    }

    @media screen and (max-width: 500px) {
        body.typora-export {
            padding-left: 0px;
            padding-right: 0px;
        }

        #write {
            padding-left: 20px;
            padding-right: 20px;
        }

        .CodeMirror-sizer {
            margin-left: 0px !important;
        }

        .CodeMirror-gutters {
            display: none !important;
        }
    }

    #write li > figure:last-child {
        margin-bottom: 0.5rem;
    }

    #write ol, #write ul {
        position: relative;
    }

    img {
        max-width: 100%;
        vertical-align: middle;
    }

    button, input, select, textarea {
        color: inherit;
        font: inherit;
    }

    input[type="checkbox"], input[type="radio"] {
        line-height: normal;
        padding: 0px;
    }

    *, ::after, ::before {
        box-sizing: border-box;
    }

    #write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre {
        width: inherit;
    }

    #write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p {
        position: relative;
    }

    p {
        line-height: inherit;
    }

    h1, h2, h3, h4, h5, h6 {
        break-after: avoid-page;
        break-inside: avoid;
        orphans: 2;
    }

    p {
        orphans: 4;
    }

    h1 {
        font-size: 2rem;
    }

    h2 {
        font-size: 1.8rem;
    }

    h3 {
        font-size: 1.6rem;
    }

    h4 {
        font-size: 1.4rem;
    }

    h5 {
        font-size: 1.2rem;
    }

    h6 {
        font-size: 1rem;
    }

    .md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p {
        margin-top: 1rem;
        margin-bottom: 1rem;
    }

    .hidden {
        display: none;
    }

    .md-blockmeta {
        color: rgb(204, 204, 204);
        font-weight: 700;
        font-style: italic;
    }

    a {
        cursor: pointer;
    }

    sup.md-footnote {
        padding: 2px 4px;
        background-color: rgba(238, 238, 238, 0.7);
        color: rgb(85, 85, 85);
        border-radius: 4px;
        cursor: pointer;
    }

    sup.md-footnote a, sup.md-footnote a:hover {
        color: inherit;
        text-transform: inherit;
        text-decoration: inherit;
    }

    #write input[type="checkbox"] {
        cursor: pointer;
        width: inherit;
        height: inherit;
    }

    figure {
        overflow-x: auto;
        margin: 1.2em 0px;
        max-width: calc(100% + 16px);
        padding: 0px;
    }

    figure > table {
        margin: 0px !important;
    }

    tr {
        break-inside: avoid;
        break-after: auto;
    }

    thead {
        display: table-header-group;
    }

    table {
        border-collapse: collapse;
        border-spacing: 0px;
        width: 100%;
        overflow: auto;
        break-inside: auto;
        text-align: left;
    }

    table.md-table td {
        min-width: 32px;
    }

    .CodeMirror-gutters {
        border-right: 0px;
        background-color: inherit;
    }

    .CodeMirror-linenumber {
        user-select: none;
    }

    .CodeMirror {
        text-align: left;
    }

    .CodeMirror-placeholder {
        opacity: 0.3;
    }

    .CodeMirror pre {
        padding: 0px 4px;
    }

    .CodeMirror-lines {
        padding: 0px;
    }

    div.hr:focus {
        cursor: none;
    }

    #write pre {
        white-space: pre-wrap;
    }

    #write.fences-no-line-wrapping pre {
        white-space: pre;
    }

    #write pre.ty-contain-cm {
        white-space: normal;
    }

    .CodeMirror-gutters {
        margin-right: 4px;
    }

    .md-fences {
        font-size: 0.9rem;
        display: block;
        break-inside: avoid;
        text-align: left;
        overflow: visible;
        white-space: pre;
        background: inherit;
        position: relative !important;
    }

    .md-diagram-panel {
        width: 100%;
        margin-top: 10px;
        text-align: center;
        padding-top: 0px;
        padding-bottom: 8px;
        overflow-x: auto;
    }

    #write .md-fences.mock-cm {
        white-space: pre-wrap;
    }

    .md-fences.md-fences-with-lineno {
        padding-left: 0px;
    }

    #write.fences-no-line-wrapping .md-fences.mock-cm {
        white-space: pre;
        overflow-x: auto;
    }

    .md-fences.mock-cm.md-fences-with-lineno {
        padding-left: 8px;
    }

    .CodeMirror-line, twitterwidget {
        break-inside: avoid;
    }

    .footnotes {
        opacity: 0.8;
        font-size: 0.9rem;
        margin-top: 1em;
        margin-bottom: 1em;
    }

    .footnotes + .footnotes {
        margin-top: 0px;
    }

    .md-reset {
        margin: 0px;
        padding: 0px;
        border: 0px;
        outline: 0px;
        vertical-align: top;
        background: 0px 0px;
        text-decoration: none;
        text-shadow: none;
        float: none;
        position: static;
        width: auto;
        height: auto;
        white-space: nowrap;
        cursor: inherit;
        -webkit-tap-highlight-color: transparent;
        line-height: normal;
        font-weight: 400;
        text-align: left;
        box-sizing: content-box;
        direction: ltr;
    }

    li div {
        padding-top: 0px;
    }

    blockquote {
        margin: 1rem 0px;
    }

    li .mathjax-block, li p {
        margin: 0.5rem 0px;
    }

    li {
        margin: 0px;
        position: relative;
    }

    blockquote > :last-child {
        margin-bottom: 0px;
    }

    blockquote > :first-child, li > :first-child {
        margin-top: 0px;
    }

    .footnotes-area {
        color: rgb(136, 136, 136);
        margin-top: 0.714rem;
        padding-bottom: 0.143rem;
        white-space: normal;
    }

    #write .footnote-line {
        white-space: pre-wrap;
    }

    @media print {
        body, html {
            border: 1px solid transparent;
            height: 99%;
            break-after: avoid;
            break-before: avoid;
        }

        #write {
            margin-top: 0px;
            padding-top: 0px;
            border-color: transparent !important;
        }

        .typora-export * {
            -webkit-print-color-adjust: exact;
        }

        html.blink-to-pdf {
            font-size: 13px;
        }

        .typora-export #write {
            padding-left: 32px;
            padding-right: 32px;
            padding-bottom: 0px;
            break-after: avoid;
        }

        .typora-export #write::after {
            height: 0px;
        }
    }

    .footnote-line {
        margin-top: 0.714em;
        font-size: 0.7em;
    }

    a img, img a {
        cursor: pointer;
    }

    pre.md-meta-block {
        font-size: 0.8rem;
        min-height: 0.8rem;
        white-space: pre-wrap;
        background: rgb(204, 204, 204);
        display: block;
        overflow-x: hidden;
    }

    p > .md-image:only-child:not(.md-img-error) img, p > img:only-child {
        display: block;
        margin: auto;
    }

    p > .md-image:only-child {
        display: inline-block;
        width: 100%;
    }

    #write .MathJax_Display {
        margin: 0.8em 0px 0px;
    }

    .md-math-block {
        width: 100%;
    }

    .md-math-block:not(:empty)::after {
        display: none;
    }

    [contenteditable="true"]:active, [contenteditable="true"]:focus, [contenteditable="false"]:active, [contenteditable="false"]:focus {
        outline: 0px;
        box-shadow: none;
    }

    .md-task-list-item {
        position: relative;
        list-style-type: none;
    }

    .task-list-item.md-task-list-item {
        padding-left: 0px;
    }

    .md-task-list-item > input {
        position: absolute;
        top: 0px;
        left: 0px;
        margin-left: -1.2em;
        margin-top: calc(1em - 10px);
        border: none;
    }

    .math {
        font-size: 1rem;
    }

    .md-toc {
        min-height: 3.58rem;
        position: relative;
        font-size: 0.9rem;
        border-radius: 10px;
    }

    .md-toc-content {
        position: relative;
        margin-left: 0px;
    }

    .md-toc-content::after, .md-toc::after {
        display: none;
    }

    .md-toc-item {
        display: block;
        color: rgb(65, 131, 196);
    }

    .md-toc-item a {
        text-decoration: none;
    }

    .md-toc-inner:hover {
        text-decoration: underline;
    }

    .md-toc-inner {
        display: inline-block;
        cursor: pointer;
    }

    .md-toc-h1 .md-toc-inner {
        margin-left: 0px;
        font-weight: 700;
    }

    .md-toc-h2 .md-toc-inner {
        margin-left: 2em;
    }

    .md-toc-h3 .md-toc-inner {
        margin-left: 4em;
    }

    .md-toc-h4 .md-toc-inner {
        margin-left: 6em;
    }

    .md-toc-h5 .md-toc-inner {
        margin-left: 8em;
    }

    .md-toc-h6 .md-toc-inner {
        margin-left: 10em;
    }

    @media screen and (max-width: 48em) {
        .md-toc-h3 .md-toc-inner {
            margin-left: 3.5em;
        }

        .md-toc-h4 .md-toc-inner {
            margin-left: 5em;
        }

        .md-toc-h5 .md-toc-inner {
            margin-left: 6.5em;
        }

        .md-toc-h6 .md-toc-inner {
            margin-left: 8em;
        }
    }

    a.md-toc-inner {
        font-size: inherit;
        font-style: inherit;
        font-weight: inherit;
        line-height: inherit;
    }

    .footnote-line a:not(.reversefootnote) {
        color: inherit;
    }

    .md-attr {
        display: none;
    }

    .md-fn-count::after {
        content: ".";
    }

    code, pre, samp, tt {
        font-family: var(--monospace);
    }

    kbd {
        margin: 0px 0.1em;
        padding: 0.1em 0.6em;
        font-size: 0.8em;
        color: rgb(36, 39, 41);
        background: rgb(255, 255, 255);
        border: 1px solid rgb(173, 179, 185);
        border-radius: 3px;
        box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset;
        white-space: nowrap;
        vertical-align: middle;
    }

    .md-comment {
        color: rgb(162, 127, 3);
        opacity: 0.8;
        font-family: var(--monospace);
    }

    code {
        text-align: left;
        vertical-align: initial;
    }

    a.md-print-anchor {
        white-space: pre !important;
        border-width: initial !important;
        border-style: none !important;
        border-color: initial !important;
        display: inline-block !important;
        position: absolute !important;
        width: 1px !important;
        right: 0px !important;
        outline: 0px !important;
        background: 0px 0px !important;
        text-decoration: initial !important;
        text-shadow: initial !important;
    }

    .md-inline-math .MathJax_SVG .noError {
        display: none !important;
    }

    .html-for-mac .inline-math-svg .MathJax_SVG {
        vertical-align: 0.2px;
    }

    .md-math-block .MathJax_SVG_Display {
        text-align: center;
        margin: 0px;
        position: relative;
        text-indent: 0px;
        max-width: none;
        max-height: none;
        min-height: 0px;
        min-width: 100%;
        width: auto;
        overflow-y: hidden;
        display: block !important;
    }

    .MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display {
        width: auto;
        margin: inherit;
        display: inline-block !important;
    }

    .MathJax_SVG .MJX-monospace {
        font-family: var(--monospace);
    }

    .MathJax_SVG .MJX-sans-serif {
        font-family: sans-serif;
    }

    .MathJax_SVG {
        display: inline;
        font-style: normal;
        font-weight: 400;
        line-height: normal;
        zoom: 90%;
        text-indent: 0px;
        text-align: left;
        text-transform: none;
        letter-spacing: normal;
        word-spacing: normal;
        overflow-wrap: normal;
        white-space: nowrap;
        float: none;
        direction: ltr;
        max-width: none;
        max-height: none;
        min-width: 0px;
        min-height: 0px;
        border: 0px;
        padding: 0px;
        margin: 0px;
    }

    .MathJax_SVG * {
        transition: none 0s ease 0s;
    }

    .MathJax_SVG_Display svg {
        vertical-align: middle !important;
        margin-bottom: 0px !important;
        margin-top: 0px !important;
    }

    .os-windows.monocolor-emoji .md-emoji {
        font-family: "Segoe UI Symbol", sans-serif;
    }

    .md-diagram-panel > svg {
        max-width: 100%;
    }

    [lang="flow"] svg, [lang="mermaid"] svg {
        max-width: 100%;
        height: auto;
    }

    [lang="mermaid"] .node text {
        font-size: 1rem;
    }

    table tr th {
        border-bottom: 0px;
    }

    video {
        max-width: 100%;
        display: block;
        margin: 0px auto;
    }

    iframe {
        max-width: 100%;
        width: 100%;
        border: none;
    }

    .highlight td, .highlight tr {
        border: 0px;
    }

    svg[id^="mermaidChart"] {
        line-height: 1em;
    }

    mark {
        background: rgb(255, 255, 0);
        color: rgb(0, 0, 0);
    }

    .md-html-inline .md-plain, .md-html-inline strong, mark .md-inline-math, mark strong {
        color: inherit;
    }

    mark .md-meta {
        color: rgb(0, 0, 0);
        opacity: 0.3 !important;
    }


    .CodeMirror {
        height: auto;
    }

    .CodeMirror.cm-s-inner {
        background: inherit;
    }

    .CodeMirror-scroll {
        overflow: auto hidden;
        z-index: 3;
    }

    .CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler {
        background-color: rgb(255, 255, 255);
    }

    .CodeMirror-gutters {
        border-right: 1px solid rgb(221, 221, 221);
        background: inherit;
        white-space: nowrap;
    }

    .CodeMirror-linenumber {
        padding: 0px 3px 0px 5px;
        text-align: right;
        color: rgb(153, 153, 153);
    }

    .cm-s-inner .cm-keyword {
        color: rgb(119, 0, 136);
    }

    .cm-s-inner .cm-atom, .cm-s-inner.cm-atom {
        color: rgb(34, 17, 153);
    }

    .cm-s-inner .cm-number {
        color: rgb(17, 102, 68);
    }

    .cm-s-inner .cm-def {
        color: rgb(0, 0, 255);
    }

    .cm-s-inner .cm-variable {
        color: rgb(0, 0, 0);
    }

    .cm-s-inner .cm-variable-2 {
        color: rgb(0, 85, 170);
    }

    .cm-s-inner .cm-variable-3 {
        color: rgb(0, 136, 85);
    }

    .cm-s-inner .cm-string {
        color: rgb(170, 17, 17);
    }

    .cm-s-inner .cm-property {
        color: rgb(0, 0, 0);
    }

    .cm-s-inner .cm-operator {
        color: rgb(152, 26, 26);
    }

    .cm-s-inner .cm-comment, .cm-s-inner.cm-comment {
        color: rgb(170, 85, 0);
    }

    .cm-s-inner .cm-string-2 {
        color: rgb(255, 85, 0);
    }

    .cm-s-inner .cm-meta {
        color: rgb(85, 85, 85);
    }

    .cm-s-inner .cm-qualifier {
        color: rgb(85, 85, 85);
    }

    .cm-s-inner .cm-builtin {
        color: rgb(51, 0, 170);
    }

    .cm-s-inner .cm-bracket {
        color: rgb(153, 153, 119);
    }

    .cm-s-inner .cm-tag {
        color: rgb(17, 119, 0);
    }

    .cm-s-inner .cm-attribute {
        color: rgb(0, 0, 204);
    }

    .cm-s-inner .cm-header, .cm-s-inner.cm-header {
        color: rgb(0, 0, 255);
    }

    .cm-s-inner .cm-quote, .cm-s-inner.cm-quote {
        color: rgb(0, 153, 0);
    }

    .cm-s-inner .cm-hr, .cm-s-inner.cm-hr {
        color: rgb(153, 153, 153);
    }

    .cm-s-inner .cm-link, .cm-s-inner.cm-link {
        color: rgb(0, 0, 204);
    }

    .cm-negative {
        color: rgb(221, 68, 68);
    }

    .cm-positive {
        color: rgb(34, 153, 34);
    }

    .cm-header, .cm-strong {
        font-weight: 700;
    }

    .cm-del {
        text-decoration: line-through;
    }

    .cm-em {
        font-style: italic;
    }

    .cm-link {
        text-decoration: underline;
    }

    .cm-error {
        color: red;
    }

    .cm-invalidchar {
        color: red;
    }

    .cm-constant {
        color: rgb(38, 139, 210);
    }

    .cm-defined {
        color: rgb(181, 137, 0);
    }

    div.CodeMirror span.CodeMirror-matchingbracket {
        color: rgb(0, 255, 0);
    }

    div.CodeMirror span.CodeMirror-nonmatchingbracket {
        color: rgb(255, 34, 34);
    }

    .cm-s-inner .CodeMirror-activeline-background {
        background: inherit;
    }

    .CodeMirror {
        position: relative;
        overflow: hidden;
    }

    .CodeMirror-scroll {
        height: 100%;
        outline: 0px;
        position: relative;
        box-sizing: content-box;
        background: inherit;
    }

    .CodeMirror-sizer {
        position: relative;
    }

    .CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar {
        position: absolute;
        z-index: 6;
        display: none;
    }

    .CodeMirror-vscrollbar {
        right: 0px;
        top: 0px;
        overflow: hidden;
    }

    .CodeMirror-hscrollbar {
        bottom: 0px;
        left: 0px;
        overflow: hidden;
    }

    .CodeMirror-scrollbar-filler {
        right: 0px;
        bottom: 0px;
    }

    .CodeMirror-gutter-filler {
        left: 0px;
        bottom: 0px;
    }

    .CodeMirror-gutters {
        position: absolute;
        left: 0px;
        top: 0px;
        padding-bottom: 30px;
        z-index: 3;
    }

    .CodeMirror-gutter {
        white-space: normal;
        height: 100%;
        box-sizing: content-box;
        padding-bottom: 30px;
        margin-bottom: -32px;
        display: inline-block;
    }

    .CodeMirror-gutter-wrapper {
        position: absolute;
        z-index: 4;
        background: 0px 0px !important;
        border: none !important;
    }

    .CodeMirror-gutter-background {
        position: absolute;
        top: 0px;
        bottom: 0px;
        z-index: 4;
    }

    .CodeMirror-gutter-elt {
        position: absolute;
        cursor: default;
        z-index: 4;
    }

    .CodeMirror-lines {
        cursor: text;
    }

    .CodeMirror pre {
        border-radius: 0px;
        border-width: 0px;
        background: 0px 0px;
        font-family: inherit;
        font-size: inherit;
        margin: 0px;
        white-space: pre;
        overflow-wrap: normal;
        color: inherit;
        z-index: 2;
        position: relative;
        overflow: visible;
    }

    .CodeMirror-wrap pre {
        overflow-wrap: break-word;
        white-space: pre-wrap;
        word-break: normal;
    }

    .CodeMirror-code pre {
        border-right: 30px solid transparent;
        width: fit-content;
    }

    .CodeMirror-wrap .CodeMirror-code pre {
        border-right: none;
        width: auto;
    }

    .CodeMirror-linebackground {
        position: absolute;
        left: 0px;
        right: 0px;
        top: 0px;
        bottom: 0px;
        z-index: 0;
    }

    .CodeMirror-linewidget {
        position: relative;
        z-index: 2;
        overflow: auto;
    }

    .CodeMirror-wrap .CodeMirror-scroll {
        overflow-x: hidden;
    }

    .CodeMirror-measure {
        position: absolute;
        width: 100%;
        height: 0px;
        overflow: hidden;
        visibility: hidden;
    }

    .CodeMirror-measure pre {
        position: static;
    }

    .CodeMirror div.CodeMirror-cursor {
        position: absolute;
        visibility: hidden;
        border-right: none;
        width: 0px;
    }

    .CodeMirror div.CodeMirror-cursor {
        visibility: hidden;
    }

    .CodeMirror-focused div.CodeMirror-cursor {
        visibility: inherit;
    }

    .cm-searching {
        background: rgba(255, 255, 0, 0.4);
    }

    @media print {
        .CodeMirror div.CodeMirror-cursor {
            visibility: hidden;
        }
    }


    :root {
        --side-bar-bg-color: #fafafa;
        --control-text-color: #777;
    }

    @include-when-export url(https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext);

    html {
        font-size: 16px;
    }

    body {
        font-family: "Courier New", "Open Sans", "Clear Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        color: rgb(51, 51, 51);
        line-height: 1.6;
    }

    #write {
        max-width: 100%;
        margin: 0 auto;
        padding: 30px;
        padding-bottom: 100px;
    }

    #write > ul:first-child,
    #write > ol:first-child {
        margin-top: 30px;
    }

    a {
        color: #4183C4;
    }

    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
        position: relative;
        margin-top: 1rem;
        margin-bottom: 1rem;
        font-weight: bold;
        line-height: 1.4;
        cursor: text;
    }

    h1:hover a.anchor,
    h2:hover a.anchor,
    h3:hover a.anchor,
    h4:hover a.anchor,
    h5:hover a.anchor,
    h6:hover a.anchor {
        text-decoration: none;
    }

    h1 tt,
    h1 code {
        font-size: inherit;
    }

    h2 tt,
    h2 code {
        font-size: inherit;
    }

    h3 tt,
    h3 code {
        font-size: inherit;
    }

    h4 tt,
    h4 code {
        font-size: inherit;
    }

    h5 tt,
    h5 code {
        font-size: inherit;
    }

    h6 tt,
    h6 code {
        font-size: inherit;
    }

    h1 {
        padding-bottom: .3em;
        font-size: 2.25em;
        line-height: 1.2;
        border-bottom: 1px solid #eee;
    }

    h2 {
        padding-bottom: .3em;
        font-size: 1.75em;
        line-height: 1.225;
        border-bottom: 1px solid #eee;
    }

    h3 {
        font-size: 1.5em;
        line-height: 1.43;
    }

    h4 {
        font-size: 1.25em;
    }

    h5 {
        font-size: 1em;
    }

    h6 {
        font-size: 1em;
        color: #777;
    }

    p,
    blockquote,
    ul,
    ol,
    dl,
    table {
        margin: 0.8em 0;
    }

    li > ol,
    li > ul {
        margin: 0 0;
    }

    hr {
        height: 2px;
        padding: 0;
        margin: 16px 0;
        background-color: #e7e7e7;
        border: 0 none;
        overflow: hidden;
        box-sizing: content-box;
    }

    li p.first {
        display: inline-block;
    }

    ul,
    ol {
        padding-left: 30px;
    }

    ul:first-child,
    ol:first-child {
        margin-top: 0;
    }

    ul:last-child,
    ol:last-child {
        margin-bottom: 0;
    }

    blockquote {
        border-left: 4px solid #dfe2e5;
        padding: 0 15px;
        color: #777777;
    }

    blockquote blockquote {
        padding-right: 0;
    }

    table {
        padding: 0;
        word-break: initial;
    }

    table tr {
        border-top: 1px solid #dfe2e5;
        margin: 0;
        padding: 0;
    }

    table tr:nth-child(2n),
    thead {
        background-color: #f8f8f8;
    }

    table tr th {
        font-weight: bold;
        border: 1px solid #dfe2e5;
        border-bottom: 0;
        margin: 0;
        padding: 6px 13px;
    }

    table tr td {
        border: 1px solid #dfe2e5;
        margin: 0;
        padding: 6px 13px;
    }

    table tr th:first-child,
    table tr td:first-child {
        margin-top: 0;
    }

    table tr th:last-child,
    table tr td:last-child {
        margin-bottom: 0;
    }

    .CodeMirror-lines {
        padding-left: 4px;
    }

    .code-tooltip {
        box-shadow: 0 1px 1px 0 rgba(0, 28, 36, .3);
        border-top: 1px solid #eef2f2;
    }

    .md-fences,
    code,
    tt {
        border: 1px solid #e7eaed;
        background-color: #f8f8f8;
        border-radius: 3px;
        padding: 0;
        padding: 2px 4px 0px 4px;
        font-size: 0.9em;
    }

    code {
        background-color: #f3f4f4;
        padding: 0 2px 0 2px;
    }

    .md-fences {
        margin-bottom: 15px;
        margin-top: 15px;
        padding-top: 8px;
        padding-bottom: 6px;
    }


    .md-task-list-item > input {
        margin-left: -1.3em;
    }

    @media print {
        html {
            font-size: 13px;
        }

        table,
        pre {
            page-break-inside: avoid;
        }

        pre {
            word-wrap: break-word;
        }
    }

    .md-fences {
        background-color: #f8f8f8;
    }

    #write pre.md-meta-block {
        padding: 1rem;
        font-size: 85%;
        line-height: 1.45;
        background-color: #f7f7f7;
        border: 0;
        border-radius: 3px;
        color: #777777;
        margin-top: 0 !important;
    }

    .mathjax-block > .code-tooltip {
        bottom: .375rem;
    }

    .md-mathjax-midline {
        background: #fafafa;
    }

    #write > h3.md-focus:before {
        left: -1.5625rem;
        top: .375rem;
    }

    #write > h4.md-focus:before {
        left: -1.5625rem;
        top: .285714286rem;
    }

    #write > h5.md-focus:before {
        left: -1.5625rem;
        top: .285714286rem;
    }

    #write > h6.md-focus:before {
        left: -1.5625rem;
        top: .285714286rem;
    }

    .md-image > .md-meta {
        /*border: 1px solid #ddd;*/
        border-radius: 3px;
        padding: 2px 0px 0px 4px;
        font-size: 0.9em;
        color: inherit;
    }

    .md-tag {
        color: #a7a7a7;
        opacity: 1;
    }

    .md-toc {
        margin-top: 20px;
        padding-bottom: 20px;
    }

    .sidebar-tabs {
        border-bottom: none;
    }

    #typora-quick-open {
        border: 1px solid #ddd;
        background-color: #f8f8f8;
    }

    #typora-quick-open-item {
        background-color: #FAFAFA;
        border-color: #FEFEFE #e5e5e5 #e5e5e5 #eee;
        border-style: solid;
        border-width: 1px;
    }

    /** focus mode */
    .on-focus-mode blockquote {
        border-left-color: rgba(85, 85, 85, 0.12);
    }

    header, .context-menu, .megamenu-content, footer {
        font-family: "Segoe UI", "Arial", sans-serif;
    }

    .file-node-content:hover .file-node-icon,
    .file-node-content:hover .file-node-open-state {
        visibility: visible;
    }

    .mac-seamless-mode #typora-sidebar {
        background-color: #fafafa;
        background-color: var(--side-bar-bg-color);
    }

    .md-lang {
        color: #b4654d;
    }

    .html-for-mac .context-menu {
        --item-hover-bg-color: #E6F0FE;
    }

    #md-notification .btn {
        border: 0;
    }

    .dropdown-menu .divider {
        border-color: #e5e5e5;
    }

    .ty-preferences .window-content {
        background-color: #fafafa;
    }

    .ty-preferences .nav-group-item.active {
        color: white;
        background: #999;
    }

    .typora-export li, .typora-export p, .typora-export, .footnote-line {
        white-space: normal;
    }
    </style>
</head>
<body class='typora-export os-windows'>
<div id='write' class='is-node'><h1><a name="1设计模式概述" class="md-header-anchor"></a><span>1，设计模式概述</span></h1>
    <h2><a name="11-软件设计模式的产生背景" class="md-header-anchor"></a><span>1.1 软件设计模式的产生背景</span></h2>
    <p><span>&quot;设计模式&quot;最初并不是出现在软件设计中，而是被用于建筑领域的设计中。</span></p>
    <p><span>1977年美国著名建筑大师、加利福尼亚大学伯克利分校环境结构中心主任</span><code>克里斯托夫·亚历山大（Christopher Alexander）</code><span>在他的著作《建筑模式语言：城镇、建筑、构造》中描述了一些常见的建筑设计问题，并提出了 253 种关于对城镇、邻里、住宅、花园和房间等进行设计的基本模式。</span>
    </p>
    <p><span>1990年软件工程界开始研讨设计模式的话题，后来召开了多次关于设计模式的研讨会。直到1995 年，艾瑞克·伽马（ErichGamma）、理査德·海尔姆（Richard Helm）、拉尔夫·约翰森（Ralph Johnson）、约翰·威利斯迪斯（John Vlissides）等 4 位作者合作出版了《设计模式：可复用面向对象软件的基础》一书，在此书中收录了 23 个设计模式，这是设计模式领域里程碑的事件，导致了软件设计模式的突破。这 4 位作者在软件开发领域里也以他们的“四人组”（Gang of Four，GoF）著称。  </span>
    </p>
    <h2><a name="12-软件设计模式的概念" class="md-header-anchor"></a><span>1.2 软件设计模式的概念</span></h2>
    <p>
        <span>软件设计模式（Software Design Pattern），又称设计模式，是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。它描述了在软件设计过程中的一些不断重复发生的问题，以及该问题的解决方案。也就是说，它是解决特定问题的一系列套路，是前辈们的代码设计经验的总结，具有一定的普遍性，可以反复使用。</span>
    </p>
    <h2><a name="13-学习设计模式的必要性" class="md-header-anchor"></a><span>1.3 学习设计模式的必要性</span></h2>
    <p><span>设计模式的本质是面向对象设计原则的实际运用，是对类的封装性、继承性和多态性以及类的关联关系和组合关系的充分理解。</span></p>
    <p><span>正确使用设计模式具有以下优点。</span></p>
    <ul>
        <li><span>可以提高程序员的思维能力、编程能力和设计能力。</span></li>
        <li><span>使程序设计更加标准化、代码编制更加工程化，使软件开发效率大大提高，从而缩短软件的开发周期。</span></li>
        <li><span>使设计的代码可重用性高、可读性强、可靠性高、灵活性好、可维护性强。</span></li>
    </ul>
    <h2><a name="14-设计模式分类" class="md-header-anchor"></a><span>1.4 设计模式分类</span></h2>
    <ul>
        <li><p><strong><span>创建型模式</span></strong></p>
            <p><span>用于描述“怎样创建对象”，它的主要特点是“将对象的创建与使用分离”。GoF（四人组）书中提供了单例、原型、工厂方法、抽象工厂、建造者等 5 种创建型模式。</span></p></li>
        <li><p><strong><span>结构型模式</span></strong></p>
            <p><span>用于描述如何将类或对象按某种布局组成更大的结构，GoF（四人组）书中提供了代理、适配器、桥接、装饰、外观、享元、组合等 7 种结构型模式。</span></p></li>
        <li><p><strong><span>行为型模式</span></strong></p>
            <p><span>用于描述类或对象之间怎样相互协作共同完成单个对象无法单独完成的任务，以及怎样分配职责。GoF（四人组）书中提供了模板方法、策略、命令、职责链、状态、观察者、中介者、迭代器、访问者、备忘录、解释器等 11 种行为型模式。</span>
            </p></li>
    </ul>
    <p>&nbsp;</p>
    <h1><a name="2uml图" class="md-header-anchor"></a><span>2，UML图</span></h1>
    <p><span>统一建模语言（Unified Modeling Language，UML）是用来设计软件的可视化建模语言。它的特点是简单、统一、图形化、能表达软件设计中的动态与静态信息。</span></p>
    <p><span>UML 从目标系统的不同角度出发，定义了用例图、类图、对象图、状态图、活动图、时序图、协作图、构件图、部署图等 9 种图。</span></p>
    <h2><a name="21-类图概述" class="md-header-anchor"></a><span>2.1 类图概述</span></h2>
    <p><span>类图(Class diagram)是显示了模型的静态结构，特别是模型中存在的类、类的内部结构以及它们与其他类的关系等。类图不显示暂时性的信息。类图是面向对象建模的主要组成部分。</span></p>
    <h2><a name="22-类图的作用" class="md-header-anchor"></a><span>2.2 类图的作用</span></h2>
    <ul>
        <li><span>在软件工程中，类图是一种静态的结构图，描述了系统的类的集合，类的属性和类之间的关系，可以简化了人们对系统的理解；</span></li>
        <li><span>类图是系统分析和设计阶段的重要产物，是系统编码和测试的重要模型。</span></li>
    </ul>
    <h2><a name="23-类图表示法" class="md-header-anchor"></a><span>2.3 类图表示法</span></h2>
    <h3><a name="231-类的表示方式" class="md-header-anchor"></a><span>2.3.1 类的表示方式 </span></h3>
    <p><span>在UML类图中，类使用包含类名、属性(field) 和方法(method) 且带有分割线的矩形来表示，比如下图表示一个Employee类，它包含name,age和address这3个属性，以及work()方法。 </span></p>
    <p><img src="img\Employee.jpg" referrerpolicy="no-referrer"></p>
    <p><span>属性/方法名称前加的加号和减号表示了这个属性/方法的可见性，UML类图中表示可见性的符号有三种：</span></p>
    <ul>
        <li><span>+：表示public</span></li>
        <li><span>-：表示private</span></li>
        <li><span>#：表示protected</span></li>
    </ul>
    <p><span>属性的完整表示方式是： </span><strong><span>可见性  名称 ：类型 [ = 缺省值]</span></strong><span>  </span></p>
    <p><span>方法的完整表示方式是： </span><strong><span>可见性  名称(参数列表) [ ： 返回类型]</span></strong></p>
    <blockquote><p><span>注意：</span></p>
        <p><span>	</span><span>1，中括号中的内容表示是可选的</span></p>
        <p><span>	</span><span>2，也有将类型放在变量名前面，返回值类型放在方法名前面</span></p></blockquote>
    <p><strong><span>举个栗子：</span></strong></p>
    <p><img src="img\demo.png" referrerpolicy="no-referrer"></p>
    <p><span>上图Demo类定义了三个方法：</span></p>
    <ul>
        <li><span>method()方法：修饰符为public，没有参数，没有返回值。</span></li>
        <li><span>method1()方法：修饰符为private，没有参数，返回值类型为String。</span></li>
        <li><span>method2()方法：修饰符为protected，接收两个参数，第一个参数类型为int，第二个参数类型为String，返回值类型是int。</span></li>
    </ul>
    <h3><a name="232-类与类之间关系的表示方式" class="md-header-anchor"></a><span>2.3.2 类与类之间关系的表示方式</span></h3><h4><a name="2321-关联关系" class="md-header-anchor"></a><span>2.3.2.1 关联关系</span>
    </h4>
    <p>
        <span>关联关系是对象之间的一种引用关系，用于表示一类对象与另一类对象之间的联系，如老师和学生、师傅和徒弟、丈夫和妻子等。关联关系是类与类之间最常用的一种关系，分为一般关联关系、聚合关系和组合关系。我们先介绍一般关联。</span>
    </p>
    <p><span>关联又可以分为单向关联，双向关联，自关联。</span></p>
    <p><strong><span>1，单向关联</span></strong></p>
    <p><img src="img\customer_address.png" referrerpolicy="no-referrer"></p>
    <p><span>在UML类图中单向关联用一个带箭头的实线表示。上图表示每个顾客都有一个地址，这通过让Customer类持有一个类型为Address的成员变量类实现。</span></p>
    <p><strong><span>2，双向关联</span></strong></p>
    <p><img src="img\customer_product.png" referrerpolicy="no-referrer"></p>
    <p><span>从上图中我们很容易看出，所谓的双向关联就是双方各自持有对方类型的成员变量。</span></p>
    <p><span>在UML类图中，双向关联用一个不带箭头的直线表示。上图中在Customer类中维护一个List</span><span>&lt;</span><span>Product&gt;，表示一个顾客可以购买多个商品；在Product类中维护一个Customer类型的成员变量表示这个产品被哪个顾客所购买。</span>
    </p>
    <p><strong><span>3，自关联</span></strong></p>
    <p><img src="img\node.png" referrerpolicy="no-referrer"></p>
    <p><span>自关联在UML类图中用一个带有箭头且指向自身的线表示。上图的意思就是Node类包含类型为Node的成员变量，也就是“自己包含自己”。</span></p>
    <p>&nbsp;</p><h4><a name="2322-聚合关系" class="md-header-anchor"></a><span>2.3.2.2 聚合关系</span></h4>
    <p><span>聚合关系是关联关系的一种，是强关联关系，是整体和部分之间的关系。</span></p>
    <p><span>聚合关系也是通过成员对象来实现的，其中成员对象是整体对象的一部分，但是成员对象可以脱离整体对象而独立存在。例如，学校与老师的关系，学校包含老师，但如果学校停办了，老师依然存在。</span>
    </p>
    <p><span>在 UML 类图中，聚合关系可以用带空心菱形的实线来表示，菱形指向整体。下图所示是大学和教师的关系图：</span></p>
    <p><img src="img\image-20191229173422328.png" referrerpolicy="no-referrer"></p><h4><a name="2323-组合关系" class="md-header-anchor"></a><span>2.3.2.3 组合关系</span></h4>
    <p><span>组合表示类之间的整体与部分的关系，但它是一种更强烈的聚合关系。</span></p>
    <p><span>在组合关系中，整体对象可以控制部分对象的生命周期，一旦整体对象不存在，部分对象也将不存在，部分对象不能脱离整体对象而存在。例如，头和嘴的关系，没有了头，嘴也就不存在了。</span></p>
    <p><span>在 UML 类图中，组合关系用带实心菱形的实线来表示，菱形指向整体。下图所示是头和嘴的关系图：</span></p>
    <p><img src="img\image-20191229173455149.png" referrerpolicy="no-referrer"></p>
    <p>&nbsp;</p><h4><a name="2324-依赖关系" class="md-header-anchor"></a><span>2.3.2.4 依赖关系</span></h4>
    <p>
        <span>依赖关系是一种使用关系，它是对象之间耦合度最弱的一种关联方式，是临时性的关联。在代码中，某个类的方法通过局部变量、方法的参数或者对静态方法的调用来访问另一个类（被依赖类）中的某些方法来完成一些职责。</span>
    </p>
    <p><span>在 UML 类图中，依赖关系使用带箭头的虚线来表示，箭头从使用类指向被依赖的类。下图所示是司机和汽车的关系图，司机驾驶汽车：</span></p>
    <p><img src="img\image-20191229173518926.png" referrerpolicy="no-referrer"></p><h4><a name="2325-继承关系" class="md-header-anchor"></a><span>2.3.2.5 继承关系</span></h4>
    <p><span>继承关系是对象之间耦合度最大的一种关系，表示一般与特殊的关系，是父类与子类之间的关系，是一种继承关系。</span></p>
    <p>
        <span>在 UML 类图中，泛化关系用带空心三角箭头的实线来表示，箭头从子类指向父类。在代码实现时，使用面向对象的继承机制来实现泛化关系。例如，Student 类和 Teacher 类都是 Person 类的子类，其类图如下图所示：</span>
    </p>
    <p><img src="img\image-20191229173539838.png" referrerpolicy="no-referrer"></p>
    <p>&nbsp;</p><h4><a name="2326-实现关系" class="md-header-anchor"></a><span>2.3.2.6 实现关系</span></h4>
    <p><span>实现关系是接口与实现类之间的关系。在这种关系中，类实现了接口，类中的操作实现了接口中所声明的所有的抽象操作。</span></p>
    <p><span>在 UML 类图中，实现关系使用带空心三角箭头的虚线来表示，箭头从实现类指向接口。例如，汽车和船实现了交通工具，其类图如图 9 所示。</span></p>
    <p><img src="img\image-20191229173554296.png" referrerpolicy="no-referrer"></p>
    <p><span>  </span></p>
    <p>&nbsp;</p>
    <h1><a name="3软件设计原则" class="md-header-anchor"></a><span>3，软件设计原则</span></h1>
    <p><span>在软件开发中，为了提高软件系统的可维护性和可复用性，增加软件的可扩展性和灵活性，程序员要尽量根据6条原则来开发程序，从而提高软件开发效率、节约软件开发成本和维护成本。</span></p>
    <h2><a name="31-开闭原则" class="md-header-anchor"></a><span>3.1 开闭原则</span></h2>
    <p><strong><span>对扩展开放，对修改关闭</span></strong><span>。在程序需要进行拓展的时候，不能去修改原有的代码，实现一个热插拔的效果。简言之，是为了使程序的扩展性好，易于维护和升级。</span></p>
    <p><span>想要达到这样的效果，我们需要使用接口和抽象类。</span></p>
    <p>
        <span>因为抽象灵活性好，适应性广，只要抽象的合理，可以基本保持软件架构的稳定。而软件中易变的细节可以从抽象派生来的实现类来进行扩展，当软件需要发生变化时，只需要根据需求重新派生一个实现类来扩展就可以了。</span>
    </p>
    <p><span>下面以 </span><code>搜狗输入法</code><span> 的皮肤为例介绍开闭原则的应用。</span></p>
    <p><span>【例】</span><code>搜狗输入法</code><span> 的皮肤设计。</span></p>
    <p><span>分析：</span><code>搜狗输入法</code><span> 的皮肤是输入法背景图片、窗口颜色和声音等元素的组合。用户可以根据自己的喜爱更换自己的输入法的皮肤，也可以从网上下载新的皮肤。这些皮肤有共同的特点，可以为其定义一个抽象类（AbstractSkin），而每个具体的皮肤（DefaultSpecificSkin和HeimaSpecificSkin）是其子类。用户窗体可以根据需要选择或者增加新的主题，而不需要修改原代码，所以它是满足开闭原则的。</span>
    </p>
    <p><img src="img\open-close.png" referrerpolicy="no-referrer"></p>
    <p>&nbsp;</p>
    <h2><a name="32-里氏代换原则" class="md-header-anchor"></a><span>3.2 里氏代换原则</span></h2>
    <p><span>里氏代换原则是面向对象设计的基本原则之一。</span></p>
    <p>
        <span>里氏代换原则：任何基类可以出现的地方，子类一定可以出现。通俗理解：子类可以扩展父类的功能，但不能改变父类原有的功能。换句话说，子类继承父类时，除添加新的方法完成新增功能外，尽量不要重写父类的方法。</span>
    </p>
    <p><span>如果通过重写父类的方法来完成新的功能，这样写起来虽然简单，但是整个继承体系的可复用性会比较差，特别是运用多态比较频繁时，程序运行出错的概率会非常大。</span></p>
    <p><span>下面看一个里氏替换原则中经典的一个例子</span></p>
    <p><span>【例】正方形不是长方形。</span></p>
    <p><span>在数学领域里，正方形毫无疑问是长方形，它是一个长宽相等的长方形。所以，我们开发的一个与几何图形相关的软件系统，就可以顺理成章的让正方形继承自长方形。</span></p>
    <p><img src="img\正方形不是长方形.png" referrerpolicy="no-referrer"></p>
    <p><span>代码如下：</span></p>
    <p><strong><span>长方形类（Rectangle）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
            class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
            autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
            style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                     cm-not-content="true"></div><div
            class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                           style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><span><span>​</span>x</span></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code"
                                                                                                                                                                       role="presentation"
                                                                                                                                                                       style=""><div
            class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
            class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter"
                                                                                                                           style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                             role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"><span
            class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">Rectangle</span> {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
            class="cm-variable-3">double</span> <span class="cm-variable">length</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                 style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable-3">double</span> <span class="cm-variable">width</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable-3">double</span> <span class="cm-variable">getLength</span>() {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">length</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">setLength</span>(<span class="cm-variable-3">double</span> <span
            class="cm-variable">length</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">this</span>.<span class="cm-variable">length</span> <span class="cm-operator">=</span> <span class="cm-variable">length</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">double</span> <span class="cm-variable">getWidth</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                      style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">return</span> <span class="cm-variable">width</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                             style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">setWidth</span>(<span class="cm-variable-3">double</span> <span class="cm-variable">width</span>) {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">this</span>.<span
            class="cm-variable">width</span> <span class="cm-operator">=</span> <span class="cm-variable">width</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 453px;"></div><div class="CodeMirror-gutters" style="height: 453px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><strong><span>正方形（Square）：</span></strong></p>
    <p><span>由于正方形的长和宽相同，所以在方法setLength和setWidth中，对长度和宽度都需要赋相同值。</span></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>12</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">Square</span> <span
            class="cm-keyword">extends</span> <span class="cm-variable">Rectangle</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                   style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span
            class="cm-variable">setWidth</span>(<span class="cm-variable-3">double</span> <span class="cm-variable">width</span>) {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">super</span>.<span
            class="cm-variable">setLength</span>(<span class="cm-variable">width</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                  style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">super</span>.<span class="cm-variable">setWidth</span>(<span class="cm-variable">width</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span
            class="cm-variable">setLength</span>(<span class="cm-variable-3">double</span> <span class="cm-variable">length</span>) {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">super</span>.<span
            class="cm-variable">setLength</span>(<span class="cm-variable">length</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                   style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">super</span>.<span class="cm-variable">setWidth</span>(<span class="cm-variable">length</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 272px;"></div><div class="CodeMirror-gutters" style="height: 272px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><span>类RectangleDemo是我们的软件系统中的一个组件，它有一个resize方法依赖基类Rectangle，resize方法是RectandleDemo类中的一个方法，用来实现宽度逐渐增长的效果。</span></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
            class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
            autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
            style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                     cm-not-content="true"></div><div
            class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                           style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>29</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">RectangleDemo</span> {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span class="cm-variable">resize</span>(<span class="cm-variable">Rectangle</span> <span
            class="cm-variable">rectangle</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">while</span> (<span class="cm-variable">rectangle</span>.<span class="cm-variable">getWidth</span>() <span class="cm-operator">&lt;=</span> <span
            class="cm-variable">rectangle</span>.<span class="cm-variable">getLength</span>()) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                         style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">rectangle</span>.<span class="cm-variable">setWidth</span>(<span class="cm-variable">rectangle</span>.<span class="cm-variable">getWidth</span>() <span
            class="cm-operator">+</span> <span class="cm-number">1</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-comment">//打印长方形的长和宽</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span class="cm-variable">printLengthAndWidth</span>(<span
            class="cm-variable">Rectangle</span> <span class="cm-variable">rectangle</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">rectangle</span>.<span
            class="cm-variable">getLength</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">rectangle</span>.<span
            class="cm-variable">getWidth</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span class="cm-variable">main</span>(<span class="cm-variable-3">String</span>[] <span
            class="cm-variable">args</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">Rectangle</span> <span class="cm-variable">rectangle</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Rectangle</span>();</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rectangle</span>.<span
            class="cm-variable">setLength</span>(<span class="cm-number">20</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                             style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">rectangle</span>.<span class="cm-variable">setWidth</span>(<span class="cm-number">10</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">resize</span>(<span
            class="cm-variable">rectangle</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">printLengthAndWidth</span>(<span class="cm-variable">rectangle</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"============"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">24</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Rectangle</span> <span
            class="cm-variable">rectangle1</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Square</span>();</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">25</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">rectangle1</span>.<span class="cm-variable">setLength</span>(<span class="cm-number">10</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">26</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">resize</span>(<span
            class="cm-variable">rectangle1</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">27</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">printLengthAndWidth</span>(<span class="cm-variable">rectangle1</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">28</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 657px;"></div><div class="CodeMirror-gutters" style="height: 657px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><span>我们运行一下这段代码就会发现，假如我们把一个普通长方形作为参数传入resize方法，就会看到长方形宽度逐渐增长的效果，当宽度大于长度,代码就会停止，这种行为的结果符合我们的预期；假如我们再把一个正方形作为参数传入resize方法后，就会看到正方形的宽度和长度都在不断增长，代码会一直运行下去，直至系统产生溢出错误。所以，普通的长方形是适合这段代码的，正方形不适合。</span>
        <span>我们得出结论：在resize方法中，Rectangle类型的参数是不能被Square类型的参数所代替，如果进行了替换就得不到预期结果。因此，Square类和Rectangle类之间的继承关系违反了里氏代换原则，它们之间的继承关系不成立，正方形不是长方形。</span>
    </p>
    <p><span>如何改进呢？此时我们需要重新设计他们之间的关系。抽象出来一个四边形接口(Quadrilateral)，让Rectangle类和Square类实现Quadrilateral接口</span></p>
    <p><img src="img\正方形不是长方形改进.png" style="zoom:80%;"/></p>
    <p>&nbsp;</p>
    <h2><a name="33-依赖倒转原则" class="md-header-anchor"></a><span>3.3 依赖倒转原则</span></h2>
    <p><span>高层模块不应该依赖低层模块，两者都应该依赖其抽象；抽象不应该依赖细节，细节应该依赖抽象。简单的说就是要求对抽象进行编程，不要对实现进行编程，这样就降低了客户与实现模块间的耦合。</span>
    </p>
    <p><span>下面看一个例子来理解依赖倒转原则</span></p>
    <p><span>【例】组装电脑</span></p>
    <p><span>现要组装一台电脑，需要配件cpu，硬盘，内存条。只有这些配置都有了，计算机才能正常的运行。选择cpu有很多选择，如Intel，AMD等，硬盘可以选择希捷，西数等，内存条可以选择金士顿，海盗船等。</span>
    </p>
    <p><strong><span>类图如下：</span></strong></p>
    <p><img src="img\依赖倒转原则.png" style="zoom:80%;"/></p>
    <p><span>代码如下：</span></p>
    <p><strong><span>希捷硬盘类（XiJieHardDisk）:</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>11</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">XiJieHardDisk</span> <span
            class="cm-keyword">implements</span> <span class="cm-variable">HardDisk</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                     style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">save</span>(<span class="cm-variable-3">String</span> <span
            class="cm-variable">data</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"使用希捷硬盘存储数据"</span> <span
            class="cm-operator">+</span> <span class="cm-variable">data</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">String</span> <span class="cm-variable">get</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"使用希捷希捷硬盘取数据"</span>);</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-string">"数据"</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 249px;"></div><div class="CodeMirror-gutters" style="height: 249px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><strong><span>Intel处理器（IntelCpu）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 36px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 28px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>6</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -28px; width: 28px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 19px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">IntelCpu</span> <span
            class="cm-keyword">implements</span> <span class="cm-variable">Cpu</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">run</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">4</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"使用Intel处理器"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">5</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 19px;">6</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 136px;"></div><div class="CodeMirror-gutters" style="height: 136px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 27px;"></div></div></div></div></pre>
    <p><strong><span>金士顿内存条（KingstonMemory）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 36px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 28px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>6</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -28px; width: 28px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 19px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">KingstonMemory</span> <span
            class="cm-keyword">implements</span> <span class="cm-variable">Memory</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                   style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">save</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">4</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"使用金士顿作为内存条"</span>);</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 19px;">5</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 19px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                             role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 136px;"></div><div class="CodeMirror-gutters" style="height: 136px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 27px;"></div></div></div></div></pre>
    <p><strong><span>电脑（Computer）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
            class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
            autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
            style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                     cm-not-content="true"></div><div
            class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                           style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>38</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">Computer</span> {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable">XiJieHardDisk</span> <span class="cm-variable">hardDisk</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
            class="cm-variable">IntelCpu</span> <span class="cm-variable">cpu</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                              style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable">KingstonMemory</span> <span class="cm-variable">memory</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable">IntelCpu</span> <span
            class="cm-variable">getCpu</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">return</span> <span class="cm-variable">cpu</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                           style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">setCpu</span>(<span class="cm-variable">IntelCpu</span> <span
            class="cm-variable">cpu</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">this</span>.<span class="cm-variable">cpu</span> <span class="cm-operator">=</span> <span class="cm-variable">cpu</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable">KingstonMemory</span> <span class="cm-variable">getMemory</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                             style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">return</span> <span class="cm-variable">memory</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                              style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">setMemory</span>(<span class="cm-variable">KingstonMemory</span> <span
            class="cm-variable">memory</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">this</span>.<span class="cm-variable">memory</span> <span class="cm-operator">=</span> <span class="cm-variable">memory</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable">XiJieHardDisk</span> <span class="cm-variable">getHardDisk</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">24</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
            class="cm-variable">hardDisk</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">26</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">27</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">setHardDisk</span>(<span class="cm-variable">XiJieHardDisk</span> <span class="cm-variable">hardDisk</span>) {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">28</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">this</span>.<span
            class="cm-variable">hardDisk</span> <span class="cm-operator">=</span> <span class="cm-variable">hardDisk</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">29</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">30</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">31</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">run</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">32</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"计算机工作"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">33</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">cpu</span>.<span
            class="cm-variable">run</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">34</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">memory</span>.<span class="cm-variable">save</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">35</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable-3">String</span> <span class="cm-variable">data</span> <span class="cm-operator">=</span> <span class="cm-variable">hardDisk</span>.<span class="cm-variable">get</span>();</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">36</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"从硬盘中获取的数据为："</span> <span class="cm-operator">+</span> <span
            class="cm-variable">data</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">37</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">38</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 861px;"></div><div class="CodeMirror-gutters" style="height: 861px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><strong><span>测试类（TestComputer）：</span></strong></p>
    <p><span>测试类用来组装电脑。</span></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>10</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
            class="cm-def">TestComputer</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span class="cm-variable">main</span>(<span class="cm-variable-3">String</span>[] <span
            class="cm-variable">args</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">Computer</span> <span class="cm-variable">computer</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Computer</span>();</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">computer</span>.<span class="cm-variable">setHardDisk</span>(<span
            class="cm-keyword">new</span> <span class="cm-variable">XiJieHardDisk</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                     style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">computer</span>.<span class="cm-variable">setCpu</span>(<span class="cm-keyword">new</span> <span class="cm-variable">IntelCpu</span>());</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">computer</span>.<span class="cm-variable">setMemory</span>(<span
            class="cm-keyword">new</span> <span class="cm-variable">KingstonMemory</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                      style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">computer</span>.<span class="cm-variable">run</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 227px;"></div><div class="CodeMirror-gutters" style="height: 227px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p>
        <span>上面代码可以看到已经组装了一台电脑，但是似乎组装的电脑的cpu只能是Intel的，内存条只能是金士顿的，硬盘只能是希捷的，这对用户肯定是不友好的，用户有了机箱肯定是想按照自己的喜好，选择自己喜欢的配件。</span>
    </p>
    <p><span>根据依赖倒转原则进行改进：</span></p>
    <p><span>代码我们只需要修改Computer类，让Computer类依赖抽象（各个配件的接口），而不是依赖于各个组件具体的实现类。</span></p>
    <p><strong><span>类图如下：</span></strong></p>
    <p><img src="img/依赖倒转原则改进.png" alt="image-20191229173554296" style="zoom:70%;"/></p>
    <p><strong><span>电脑（Computer）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
            class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
            autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
            style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                     cm-not-content="true"></div><div
            class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                           style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>34</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">Computer</span> {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable">HardDisk</span> <span class="cm-variable">hardDisk</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
            class="cm-variable">Cpu</span> <span class="cm-variable">cpu</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable">Memory</span> <span class="cm-variable">memory</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable">HardDisk</span> <span class="cm-variable">getHardDisk</span>() {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">hardDisk</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">setHardDisk</span>(<span class="cm-variable">HardDisk</span> <span
            class="cm-variable">hardDisk</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">this</span>.<span class="cm-variable">hardDisk</span> <span class="cm-operator">=</span> <span class="cm-variable">hardDisk</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable">Cpu</span> <span class="cm-variable">getCpu</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">return</span> <span class="cm-variable">cpu</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                           style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">setCpu</span>(<span class="cm-variable">Cpu</span> <span class="cm-variable">cpu</span>) {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">this</span>.<span class="cm-variable">cpu</span> <span class="cm-operator">=</span> <span
            class="cm-variable">cpu</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable">Memory</span> <span class="cm-variable">getMemory</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                     style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">24</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">return</span> <span class="cm-variable">memory</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                              style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">26</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">27</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">setMemory</span>(<span class="cm-variable">Memory</span> <span class="cm-variable">memory</span>) {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">28</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">this</span>.<span
            class="cm-variable">memory</span> <span class="cm-operator">=</span> <span class="cm-variable">memory</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">29</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">30</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">31</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">run</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">32</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"计算机工作"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">33</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">34</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 771px;"></div><div class="CodeMirror-gutters" style="height: 771px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p>
        <span>面向对象的开发很好的解决了这个问题，一般情况下抽象的变化概率很小，让用户程序依赖于抽象，实现的细节也依赖于抽象。即使实现细节不断变动，只要抽象不变，客户程序就不需要变化。这大大降低了客户程序与实现细节的耦合度。</span>
    </p>
    <p>&nbsp;</p>
    <h2><a name="34-接口隔离原则" class="md-header-anchor"></a><span>3.4 接口隔离原则</span></h2>
    <p><span>客户端不应该被迫依赖于它不使用的方法；一个类对另一个类的依赖应该建立在最小的接口上。</span></p>
    <p><span>下面看一个例子来理解接口隔离原则</span></p>
    <p><span>【例】安全门案例</span></p>
    <p><span>我们需要创建一个</span><code>黑马</code><span>品牌的安全门，该安全门具有防火、防水、防盗的功能。可以将防火，防水，防盗功能提取成一个接口，形成一套规范。类图如下：</span></p>
    <p><img src="img\接口隔离原则.png" referrerpolicy="no-referrer"></p>
    <p>
        <span>上面的设计我们发现了它存在的问题，黑马品牌的安全门具有防盗，防水，防火的功能。现在如果我们还需要再创建一个传智品牌的安全门，而该安全门只具有防盗、防水功能呢？很显然如果实现SafetyDoor接口就违背了接口隔离原则，那么我们如何进行修改呢？看如下类图：</span>
    </p>
    <p><img src="img\接口隔离原则1.png" referrerpolicy="no-referrer"></p>
    <p><span>代码如下：</span></p>
    <p><strong><span>AntiTheft（接口）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 36px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 28px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>3</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline"
                                                                                                                                                      style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -28px; width: 28px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 19px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">interface</span> <span class="cm-def">AntiTheft</span> {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 19px;">2</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">void</span> <span class="cm-variable">antiTheft</span>();</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 19px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 68px;"></div><div class="CodeMirror-gutters" style="height: 68px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 27px;"></div></div></div></div></pre>
    <p><strong><span>Fireproof（接口）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 36px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 28px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>3</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline"
                                                                                                                                                      style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -28px; width: 28px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 19px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">interface</span> <span class="cm-def">Fireproof</span> {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 19px;">2</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">void</span> <span class="cm-variable">fireproof</span>();</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 19px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 68px;"></div><div class="CodeMirror-gutters" style="height: 68px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 27px;"></div></div></div></div></pre>
    <p><strong><span>Waterproof（接口）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 36px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 28px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>3</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline"
                                                                                                                                                      style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -28px; width: 28px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 19px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">interface</span> <span class="cm-def">Waterproof</span> {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 19px;">2</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">void</span> <span class="cm-variable">waterproof</span>();</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 19px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 68px;"></div><div class="CodeMirror-gutters" style="height: 68px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 27px;"></div></div></div></div></pre>
    <p><strong><span>HeiMaSafetyDoor（类）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
            class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
            autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
            style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                     cm-not-content="true"></div><div
            class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                           style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>14</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">HeiMaSafetyDoor</span> <span
            class="cm-keyword">implements</span> <span class="cm-variable">AntiTheft</span>,<span class="cm-variable">Fireproof</span>,<span
            class="cm-variable">Waterproof</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">antiTheft</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"防盗"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span
            class="cm-variable">fireproof</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"防火"</span>);</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">waterproof</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"防水"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 317px;"></div><div class="CodeMirror-gutters" style="height: 317px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><strong><span>ItcastSafetyDoor（类）：</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 36px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 28px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>9</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -28px; width: 28px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 19px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">ItcastSafetyDoor</span> <span
            class="cm-keyword">implements</span> <span class="cm-variable">AntiTheft</span>,<span class="cm-variable">Fireproof</span> {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">2</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">antiTheft</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                     style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"防盗"</span>);</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 19px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">fireproof</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">7</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-string">"防火"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">8</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 19px;">9</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 204px;"></div><div class="CodeMirror-gutters" style="height: 204px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 27px;"></div></div></div></div></pre>
    <p>&nbsp;</p>
    <h2><a name="35-迪米特法则" class="md-header-anchor"></a><span>3.5 迪米特法则</span></h2>
    <p><span>迪米特法则又叫最少知识原则。</span></p>
    <p><span>只和你的直接朋友交谈，不跟“陌生人”说话（Talk only to your immediate friends and not to strangers）。</span></p>
    <p><span>其含义是：如果两个软件实体无须直接通信，那么就不应当发生直接的相互调用，可以通过第三方转发该调用。其目的是降低类之间的耦合度，提高模块的相对独立性。</span></p>
    <p><span>迪米特法则中的“朋友”是指：当前对象本身、当前对象的成员对象、当前对象所创建的对象、当前对象的方法参数等，这些对象同当前对象存在关联、聚合或组合关系，可以直接访问这些对象的方法。</span>
    </p>
    <p><span>下面看一个例子来理解迪米特法则</span></p>
    <p><span>【例】明星与经纪人的关系实例</span></p>
    <p><span>明星由于全身心投入艺术，所以许多日常事务由经纪人负责处理，如和粉丝的见面会，和媒体公司的业务洽淡等。这里的经纪人是明星的朋友，而粉丝和媒体公司是陌生人，所以适合使用迪米特法则。</span>
    </p>
    <p><span>类图如下：</span></p>
    <p><img src="img/迪米特法则.png" alt="image-20191229173554296" style="zoom:80%;"/></p>
    <p><span>代码如下：</span></p>
    <p><strong><span>明星类（Star）</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>11</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
            class="cm-def">Star</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable-3">String</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable">Star</span>(<span
            class="cm-variable-3">String</span> <span class="cm-variable">name</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                 style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">this</span>.<span class="cm-variable">name</span><span class="cm-operator">=</span><span class="cm-variable">name</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">String</span> <span class="cm-variable">getName</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
            class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 249px;"></div><div class="CodeMirror-gutters" style="height: 249px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><strong><span>粉丝类（Fans）</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>11</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
            class="cm-def">Fans</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable-3">String</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable">Fans</span>(<span
            class="cm-variable-3">String</span> <span class="cm-variable">name</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                 style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">this</span>.<span class="cm-variable">name</span><span class="cm-operator">=</span><span class="cm-variable">name</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">String</span> <span class="cm-variable">getName</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
            class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 249px;"></div><div class="CodeMirror-gutters" style="height: 249px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><strong><span>媒体公司类（Company）</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
            style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                                                                                                                   style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
            class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div
            class="CodeMirror-sizer" style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>11</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">Company</span> {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-variable-3">String</span> <span
            class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable">Company</span>(<span class="cm-variable-3">String</span> <span class="cm-variable">name</span>) {</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">this</span>.<span class="cm-variable">name</span><span class="cm-operator">=</span><span
            class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">String</span> <span class="cm-variable">getName</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
            class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                              role="presentation"><span role="presentation"
                                                                                                                                                                        style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 249px;"></div><div class="CodeMirror-gutters" style="height: 249px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p><strong><span>经纪人类（Agent）</span></strong></p>
    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
            class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
            autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
            style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                     cm-not-content="true"></div><div
            class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                           style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
            style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
            class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>25</div></div></div><div
            class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline"
                                                                                                                                                               style="position: relative;"><div
            class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter"
                                                                                          style="left: -36px; width: 36px;"></div><div
            class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                     style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                       role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
            class="cm-def">Agent</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable">Star</span> <span class="cm-variable">star</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
            class="cm-variable">Fans</span> <span class="cm-variable">fans</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                           style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                      style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">private</span> <span class="cm-variable">Company</span> <span class="cm-variable">company</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                         style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                           role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span
            class="cm-variable">setStar</span>(<span class="cm-variable">Star</span> <span class="cm-variable">star</span>) {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">this</span>.<span
            class="cm-variable">star</span> <span class="cm-operator">=</span> <span class="cm-variable">star</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                         style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                            role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span
            class="cm-variable">setFans</span>(<span class="cm-variable">Fans</span> <span class="cm-variable">fans</span>) {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">this</span>.<span
            class="cm-variable">fans</span> <span class="cm-operator">=</span> <span class="cm-variable">fans</span>;</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">setCompany</span>(<span class="cm-variable">Company</span> <span
            class="cm-variable">company</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-keyword">this</span>.<span class="cm-variable">company</span> <span class="cm-operator">=</span> <span class="cm-variable">company</span>;</span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
            style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
            class="cm-variable-3">void</span> <span class="cm-variable">meeting</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                   style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
            class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">fans</span>.<span class="cm-variable">getName</span>() <span
            class="cm-operator">+</span> <span class="cm-string">"与明星"</span> <span class="cm-operator">+</span> <span class="cm-variable">star</span>.<span
            class="cm-variable">getName</span>() <span class="cm-operator">+</span> <span class="cm-string">"见面了。"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"><span
            cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
            class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                                                                                                                       style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
            class="cm-keyword">public</span> <span class="cm-variable-3">void</span> <span class="cm-variable">business</span>() {</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span
            class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">company</span>.<span class="cm-variable">getName</span>() <span
            class="cm-operator">+</span> <span class="cm-string">"与明星"</span> <span class="cm-operator">+</span> <span class="cm-variable">star</span>.<span
            class="cm-variable">getName</span>() <span class="cm-operator">+</span> <span class="cm-string">"洽淡业务。"</span>);</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">24</div></div><pre
            class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
            class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                        style="left: 0px; width: 27px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span
            role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
            style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 567px;"></div><div class="CodeMirror-gutters" style="height: 567px;"><div
            class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
    <p>&nbsp;</p>
    <h2><a name="36-合成复用原则" class="md-header-anchor"></a><span>3.6 合成复用原则</span></h2>
    <p><span>合成复用原则是指：尽量先使用组合或者聚合等关联关系来实现，其次才考虑使用继承关系来实现。</span></p>
    <p><span>通常类的复用分为继承复用和合成复用两种。</span></p>
    <p><span>继承复用虽然有简单和易实现的优点，但它也存在以下缺点：</span></p>
    <ol start=''>
        <li><span>继承复用破坏了类的封装性。因为继承会将父类的实现细节暴露给子类，父类对子类是透明的，所以这种复用又称为“白箱”复用。</span></li>
        <li><span>子类与父类的耦合度高。父类的实现的任何改变都会导致子类的实现发生变化，这不利于类的扩展与维护。</span></li>
        <li><span>它限制了复用的灵活性。从父类继承而来的实现是静态的，在编译时已经定义，所以在运行时不可能发生变化。</span></li>
    </ol>
    <p><span>采用组合或聚合复用时，可以将已有对象纳入新对象中，使之成为新对象的一部分，新对象可以调用已有对象的功能，它有以下优点：</span></p>
    <ol start=''>
        <li><span>它维持了类的封装性。因为成分对象的内部细节是新对象看不见的，所以这种复用又称为“黑箱”复用。</span></li>
        <li><span>对象间的耦合度低。可以在类的成员位置声明抽象。</span></li>
        <li><span>复用的灵活性高。这种复用可以在运行时动态进行，新对象可以动态地引用与成分对象类型相同的对象。</span></li>
    </ol>
    <p><span>下面看一个例子来理解合成复用原则</span></p>
    <p><span>【例】汽车分类管理程序</span></p>
    <p><span>汽车按“动力源”划分可分为汽油汽车、电动汽车等；按“颜色”划分可分为白色汽车、黑色汽车和红色汽车等。如果同时考虑这两种分类，其组合就很多。类图如下： </span></p>
    <p><img src="img/合成复用原则.png" alt="image-20191229173554296" style="zoom:80%;"/></p>
    <p><span>从上面类图我们可以看到使用继承复用产生了很多子类，如果现在又有新的动力源或者新的颜色的话，就需要再定义新的类。我们试着将继承复用改为聚合复用看一下。</span></p>
    <p><img src="img/合成复用原则1.png" alt="image-20191229173554296" style="zoom:80%;"/></p>
    <p>&nbsp;</p>
    <h1><a name="4创建者模式" class="md-header-anchor"></a><span>4，创建者模式</span></h1>
    <p><span>创建型模式的主要关注点是“怎样创建对象？”，它的主要特点是“将对象的创建与使用分离”。</span></p>
    <p><span>这样可以降低系统的耦合度，使用者不需要关注对象的创建细节。</span></p>
    <p><span>创建型模式分为：</span></p>
    <ul>
        <li><span>单例模式</span></li>
        <li><span>工厂方法模式</span></li>
        <li><span>抽象工程模式</span></li>
        <li><span>原型模式</span></li>
        <li><span>建造者模式</span></li>
    </ul>
    <h2><a name="41-单例设计模式" class="md-header-anchor"></a><span>4.1 单例设计模式</span></h2>
    <p><span>单例模式（Singleton Pattern）是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式，它提供了一种创建对象的最佳方式。</span></p>
    <p><span>这种模式涉及到一个单一的类，该类负责创建自己的对象，同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式，可以直接访问，不需要实例化该类的对象。</span></p>
    <h3><a name="411-单例模式的结构" class="md-header-anchor"></a><span>4.1.1 单例模式的结构</span></h3>
    <p><span>单例模式的主要有以下角色：</span></p>
    <ul>
        <li><span>单例类。只能创建一个实例的类</span></li>
        <li><span>访问类。使用单例类</span></li>
    </ul>
    <h3><a name="412-单例模式的实现" class="md-header-anchor"></a><span>4.1.2 单例模式的实现</span></h3>
    <blockquote><p><span>单例设计模式分类两种：</span></p>
        <p><span>	</span><span>饿汉式：类加载就会导致该单实例对象被创建</span><span>	</span></p>
        <p><span>	</span><span>懒汉式：类加载不会导致该单实例对象被创建，而是首次使用该对象时才会创建</span></p></blockquote>
    <ol start=''>
        <li><p><span>饿汉式-方式1（静态变量方式）</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>16</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 饿汉式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* &nbsp; &nbsp;  静态变量创建类的对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//在成员位置创建该类的对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">instance</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span
                    class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 363px;"></div><div class="CodeMirror-gutters" style="height: 363px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><font color='red'><span>说明：</span></font></p>
            <p>
                <span>	</span><span>该方式在成员位置声明Singleton类型的静态变量，并创建Singleton类的对象instance。instance对象是随着类的加载而创建的。如果该对象足够大的话，而一直没有使用就会造成内存的浪费。</span>
            </p>
            <p>&nbsp;</p></li>
        <li><p><span>饿汉式-方式2（静态代码块方式）</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>21</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 恶汉式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* &nbsp; &nbsp;  在静态代码块中创建该类对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//在成员位置创建该类的对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">static</span> {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-variable">instance</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span
                    class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 476px;"></div><div class="CodeMirror-gutters" style="height: 476px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><font color='red'><span>说明：</span></font></p>
            <p>
                <span>	</span><span>该方式在成员位置声明Singleton类型的静态变量，而对象的创建是在静态代码块中，也是对着类的加载而创建。所以和饿汉式的方式1基本上一样，当然该方式也存在内存浪费问题。</span>
            </p>
            <p>&nbsp;</p></li>
        <li><p><span>懒汉式-方式1（线程不安全）</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>20</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 懒汉式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*  线程不安全</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//在成员位置创建该类的对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span class="cm-variable">instance</span> <span
                    class="cm-operator">==</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 453px;"></div><div class="CodeMirror-gutters" style="height: 453px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><font color='red'><span>说明：</span></font></p>
            <p>
                <span>	</span><span>从上面代码我们可以看出该方式在成员位置声明Singleton类型的静态变量，并没有进行对象的赋值操作，那么什么时候赋值的呢？当调用getInstance()方法获取Singleton类的对象的时候才创建Singleton类的对象，这样就实现了懒加载的效果。但是，如果是多线程环境，会出现线程安全问题。</span>
            </p>
            <p>&nbsp;</p></li>
        <li><p><span>懒汉式-方式2（线程安全）</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>20</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 懒汉式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*  线程安全</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//在成员位置创建该类的对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-keyword">synchronized</span> <span
                    class="cm-variable">Singleton</span> <span class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span class="cm-variable">instance</span> <span
                    class="cm-operator">==</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 453px;"></div><div class="CodeMirror-gutters" style="height: 453px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><font color='red'><span>说明：</span></font></p>
            <p>
                <span>	</span><span>该方式也实现了懒加载效果，同时又解决了线程安全问题。但是在getInstance()方法上添加了synchronized关键字，导致该方法的执行效果特别低。从上面代码我们可以看出，其实就是在初始化instance的时候才会出现线程安全问题，一旦初始化完成就不存在了。</span>
            </p>
            <p>&nbsp;</p></li>
        <li><p><span>懒汉式-方式3（双重检查锁）</span></p>
            <p>
                <span>再来讨论一下懒汉模式中加锁的问题，对于 </span><code>getInstance()</code><span> 方法来说，绝大部分的操作都是读操作，读操作是线程安全的，所以我们没必让每个线程必须持有锁才能调用该方法，我们需要调整加锁的时机。由此也产生了一种新的实现模式：双重检查锁模式</span>
            </p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>24</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 双重检查方式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> { </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; <span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
                    class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span class="cm-variable">getInstance</span>() {</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation"
                                                                                                                                               cm-text="	">    </span><span
                    class="cm-comment">//第一次判断，如果instance不为null，不进入抢锁阶段，直接返回实例</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span
                    class="cm-variable">instance</span> <span class="cm-operator">==</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">synchronized</span> (<span
                    class="cm-variable">Singleton</span>.<span class="cm-keyword">class</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                          style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-comment">//抢到锁之后再次判断是否为null</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span class="cm-variable">instance</span> <span
                    class="cm-operator">==</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                        style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">22</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">24</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 544px;"></div><div class="CodeMirror-gutters" style="height: 544px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p>
                <span>双重检查锁模式是一种非常好的单例实现模式，解决了单例、性能、线程安全问题，上面的双重检测锁模式看上去完美无缺，其实是存在问题，在多线程的情况下，可能会出现空指针问题，出现问题的原因是JVM在实例化对象的时候会进行优化和指令重排序操作。</span>
            </p>
            <p><span>要解决双重检查锁模式带来空指针异常的问题，只需要使用 </span><code>volatile</code><span> 关键字, </span><code>volatile</code><span> 关键字可以保证可见性和有序性。</span>
            </p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>24</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 双重检查方式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-keyword">volatile</span> <span
                    class="cm-variable">Singleton</span> <span class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                            style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; <span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
                    class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span class="cm-variable">getInstance</span>() {</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation"
                                                                                                                                               cm-text="	">    </span><span
                    class="cm-comment">//第一次判断，如果instance不为null，不进入抢锁阶段，直接返回实际</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span
                    class="cm-variable">instance</span> <span class="cm-operator">==</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">synchronized</span> (<span
                    class="cm-variable">Singleton</span>.<span class="cm-keyword">class</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                          style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-comment">//抢到锁之后再次判断是否为空</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span class="cm-variable">instance</span> <span
                    class="cm-operator">==</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation"
                                                                        style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">22</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">24</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 544px;"></div><div class="CodeMirror-gutters" style="height: 544px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><font color="red"><span>小结：</span></font></p>
            <p><span>添加 </span><code>volatile</code><span> 关键字之后的双重检查锁模式是一种比较好的单例实现模式，能够保证在多线程的情况下线程安全也不会有性能问题。</span></p></li>
    </ol>
    <p>&nbsp;</p>
    <ol start='6'>
        <li><p><span>懒汉式-方式4（静态内部类方式）</span></p>
            <p>
                <span>静态内部类单例模式中实例由内部类创建，由于 JVM 在加载外部类的过程中, 是不会加载静态内部类的, 只有内部类的属性/方法被调用时才会被加载, 并初始化其静态属性。静态属性由于被 </span><code>static</code><span> 修饰，保证只被实例化一次，并且严格保证实例化顺序。</span>
            </p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>17</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 静态内部类方式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">SingletonHolder</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span
                    class="cm-keyword">final</span> <span class="cm-variable">Singleton</span> <span class="cm-variable">INSTANCE</span> <span class="cm-operator">=</span> <span
                    class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">SingletonHolder</span>.<span class="cm-variable">INSTANCE</span>;</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 385px;"></div><div class="CodeMirror-gutters" style="height: 385px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><font color='red'><span>说明：</span></font></p>
            <p><span>	</span><span>第一次加载Singleton类时不会去初始化INSTANCE，只有第一次调用getInstance，虚拟机加载SingletonHolder</span></p>
            <p><span>并初始化INSTANCE，这样不仅能确保线程安全，也能保证 Singleton 类的唯一性。</span></p>
            <p><font color="red"><span>小结：</span></font></p>
            <p>
                <span>	</span><span>静态内部类单例模式是一种优秀的单例模式，是开源项目中比较常用的一种单例模式。在没有加任何锁的情况下，保证了多线程下的安全，并且没有任何性能影响和空间的浪费。</span>
            </p>
            <p>&nbsp;</p></li>
        <li><p><span>枚举方式</span></p>
            <p>
                <span>枚举类实现单例模式是极力推荐的单例实现模式，因为枚举类型是线程安全的，并且只会装载一次，设计者充分的利用了枚举的这个特性来实现单例模式，枚举的写法非常简单，而且枚举类型是所用单例实现中唯一一种不会被破坏的单例实现模式。</span>
            </p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div
                    style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 36px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false"
                                                                                                                           tabindex="0"
                                                                                                                           style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div
                    class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll"
                                                                                                                                                           tabindex="-1"><div
                    class="CodeMirror-sizer" style="margin-left: 28px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>6</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -28px; width: 28px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 19px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* 枚举方式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 19px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span
                    class="cm-keyword">enum</span> <span class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                   style="left: -28px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 19px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">INSTANCE</span>;</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -28px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                style="left: 0px; width: 19px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 136px;"></div><div class="CodeMirror-gutters" style="height: 136px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 27px;"></div></div></div></div></pre>
            <p><font color='red'><span>说明：</span></font></p>
            <p><span>	</span><span>枚举方式属于恶汉式方式。</span></p></li>
    </ol>
    <p>&nbsp;</p>
    <h3><a name="413-存在的问题" class="md-header-anchor"></a><span>4.1.3 存在的问题</span></h3><h4><a name="4131-问题演示" class="md-header-anchor"></a><span>4.1.3.1 问题演示</span></h4>
    <p><span>破坏单例模式：</span></p>
    <p><span>使上面定义的单例类（Singleton）可以创建多个对象，枚举方式除外。有两种方式，分别是序列化和反射。</span></p>
    <ul>
        <li><p><span>序列化反序列化</span></p>
            <p><strong><span>Singleton类：</span></strong></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>14</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">Singleton</span> <span
                    class="cm-keyword">implements</span> <span class="cm-variable">Serializable</span> {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">SingletonHolder</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span
                    class="cm-keyword">final</span> <span class="cm-variable">Singleton</span> <span class="cm-variable">INSTANCE</span> <span class="cm-operator">=</span> <span
                    class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">SingletonHolder</span>.<span class="cm-variable">INSTANCE</span>;</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 317px;"></div><div class="CodeMirror-gutters" style="height: 317px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><strong><span>Test类：</span></strong></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>30</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Test</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span
                    class="cm-variable">main</span>(<span class="cm-variable-3">String</span>[] <span class="cm-variable">args</span>) <span class="cm-keyword">throws</span> <span
                    class="cm-variable">Exception</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//往文件中写对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//writeObject2File();</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//从文件中读取对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Singleton</span> <span class="cm-variable">s1</span> <span
                    class="cm-operator">=</span> <span class="cm-variable">readObjectFromFile</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-variable">Singleton</span> <span class="cm-variable">s2</span> <span class="cm-operator">=</span> <span class="cm-variable">readObjectFromFile</span>();</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-comment">//判断两个反序列化后的对象是否是同一个对象</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                 style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span
                    class="cm-variable">println</span>(<span class="cm-variable">s1</span> <span class="cm-operator">==</span> <span class="cm-variable">s2</span>);</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">readObjectFromFile</span>() <span class="cm-keyword">throws</span> <span class="cm-variable">Exception</span> {</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//创建对象输入流对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">ObjectInputStream</span> <span
                    class="cm-variable">ois</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">ObjectInputStream</span>(<span
                    class="cm-keyword">new</span> <span class="cm-variable">FileInputStream</span>(<span class="cm-string">"C:\\Users\\Think\\Desktop\\a.txt"</span>));</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//第一个读取Singleton对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Singleton</span> <span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> (<span class="cm-variable">Singleton</span>) <span class="cm-variable">ois</span>.<span class="cm-variable">readObject</span>();</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span
                    class="cm-variable">writeObject2File</span>() <span class="cm-keyword">throws</span> <span class="cm-variable">Exception</span> {</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">23</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//获取Singleton类的对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">24</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Singleton</span> <span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> <span class="cm-variable">Singleton</span>.<span class="cm-variable">getInstance</span>();</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">25</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//创建对象输出流</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">26</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">ObjectOutputStream</span> <span
                    class="cm-variable">oos</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">ObjectOutputStream</span>(<span
                    class="cm-keyword">new</span> <span class="cm-variable">FileOutputStream</span>(<span class="cm-string">"C:\\Users\\Think\\Desktop\\a.txt"</span>));</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">27</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//将instance对象写出到文件中</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">28</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">oos</span>.<span class="cm-variable">writeObject</span>(<span
                    class="cm-variable">instance</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">30</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 725px;"></div><div class="CodeMirror-gutters" style="height: 725px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <blockquote><p><span>上面代码运行结果是</span><code>false</code><span>，表明序列化和反序列化已经破坏了单例设计模式。</span></p></blockquote>
        </li>
        <li><p><span>反射</span></p>
            <p><strong><span>Singleton类：</span></strong></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>23</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                             style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-keyword">volatile</span> <span
                    class="cm-variable">Singleton</span> <span class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                            style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span class="cm-variable">instance</span> <span
                    class="cm-operator">!=</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">synchronized</span> (<span
                    class="cm-variable">Singleton</span>.<span class="cm-keyword">class</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                          style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span
                    class="cm-variable">instance</span> <span class="cm-operator">!=</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-keyword">return</span> <span class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">23</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 521px;"></div><div class="CodeMirror-gutters" style="height: 521px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><strong><span>Test类：</span></strong></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>18</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Test</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span
                    class="cm-variable">main</span>(<span class="cm-variable-3">String</span>[] <span class="cm-variable">args</span>) <span class="cm-keyword">throws</span> <span
                    class="cm-variable">Exception</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//获取Singleton类的字节码对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Class</span> <span class="cm-variable">clazz</span> <span
                    class="cm-operator">=</span> <span class="cm-variable">Singleton</span>.<span class="cm-keyword">class</span>;</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//获取Singleton类的私有无参构造方法对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Constructor</span> <span
                    class="cm-variable">constructor</span> <span class="cm-operator">=</span> <span class="cm-variable">clazz</span>.<span class="cm-variable">getDeclaredConstructor</span>();</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//取消访问检查</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">constructor</span>.<span class="cm-variable">setAccessible</span>(<span
                    class="cm-atom">true</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//创建Singleton类的对象s1</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Singleton</span> <span class="cm-variable">s1</span> <span
                    class="cm-operator">=</span> (<span class="cm-variable">Singleton</span>) <span class="cm-variable">constructor</span>.<span
                    class="cm-variable">newInstance</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//创建Singleton类的对象s2</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Singleton</span> <span class="cm-variable">s2</span> <span
                    class="cm-operator">=</span> (<span class="cm-variable">Singleton</span>) <span class="cm-variable">constructor</span>.<span
                    class="cm-variable">newInstance</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//判断通过反射创建的两个Singleton对象是否是同一个对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span
                    class="cm-variable">println</span>(<span class="cm-variable">s1</span> <span class="cm-operator">==</span> <span class="cm-variable">s2</span>);</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 408px;"></div><div class="CodeMirror-gutters" style="height: 408px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <blockquote><p><span>上面代码运行结果是</span><code>false</code><span>，表明序列化和反序列化已经破坏了单例设计模式</span></p></blockquote>
        </li>
    </ul>
    <blockquote><p><font color="red"><span>注意：</span></font><span>枚举方式不会出现这两个问题。</span></p></blockquote>
    <p>&nbsp;</p><h4><a name="4132-问题的解决" class="md-header-anchor"></a><span>4.1.3.2 问题的解决</span></h4>
    <ul>
        <li><p><span>序列化、反序列方式破坏单例模式的解决方法</span></p>
            <p><span>在Singleton类中添加</span><code>readResolve()</code><span>方法，在反序列化时被反射调用，如果定义了这个方法，就返回这个方法的值，如果没有定义，则返回新new出来的对象。</span>
            </p>
            <p><strong><span>Singleton类：</span></strong></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>21</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">Singleton</span> <span
                    class="cm-keyword">implements</span> <span class="cm-variable">Serializable</span> {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">SingletonHolder</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span
                    class="cm-keyword">final</span> <span class="cm-variable">Singleton</span> <span class="cm-variable">INSTANCE</span> <span class="cm-operator">=</span> <span
                    class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">SingletonHolder</span>.<span class="cm-variable">INSTANCE</span>;</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/**</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">* 下面是为了解决序列化反序列化破解单例模式</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable-3">Object</span> <span class="cm-variable">readResolve</span>() {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-keyword">return</span> <span class="cm-variable">SingletonHolder</span>.<span class="cm-variable">INSTANCE</span>;</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 476px;"></div><div class="CodeMirror-gutters" style="height: 476px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><strong><span>源码解析：</span></strong></p>
            <p><span>ObjectInputStream类</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>38</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">final</span> <span
                    class="cm-variable-3">Object</span> <span class="cm-def">readObject</span>() <span class="cm-keyword">throws</span> <span class="cm-variable">IOException</span>, <span
                    class="cm-variable">ClassNotFoundException</span>{</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  ...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                           style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
                    class="cm-comment">// if nested read, passHandle contains handle of enclosing object</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span
                    class="cm-variable">outerHandle</span> <span class="cm-operator">=</span> <span class="cm-variable">passHandle</span>;</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
                    class="cm-keyword">try</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable-3">Object</span> <span class="cm-variable">obj</span> <span
                    class="cm-operator">=</span> <span class="cm-variable">readObject0</span>(<span class="cm-atom">false</span>);<span
                    class="cm-comment">//重点查看readObject0方法</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  .....</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                             style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                             style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">private</span> <span class="cm-variable-3">Object</span> <span
                    class="cm-variable">readObject0</span>(<span class="cm-variable-3">boolean</span> <span class="cm-variable">unshared</span>) <span class="cm-keyword">throws</span> <span
                    class="cm-variable">IOException</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span>...</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">try</span> {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation"
                                                                                                                                cm-text="	">    </span><span class="cm-tab"
                                                                                                                                                                role="presentation"
                                                                                                                                                                cm-text="	">    </span><span
                    class="cm-keyword">switch</span> (<span class="cm-variable">tc</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                     style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation"
                                                                                                                                               cm-text="	">    </span><span class="cm-tab"
                                                                                                                                                                               role="presentation"
                                                                                                                                                                               cm-text="	">    </span>...</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation"
                                                                                                                                               cm-text="	">    </span><span class="cm-tab"
                                                                                                                                                                               role="presentation"
                                                                                                                                                                               cm-text="	">    </span><span
                    class="cm-keyword">case</span> <span class="cm-variable">TC_OBJECT</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation"
                                                                                                                                               cm-text="	">    </span><span class="cm-tab"
                                                                                                                                                                               role="presentation"
                                                                                                                                                                               cm-text="	">    </span><span
                    class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">return</span> <span class="cm-variable">checkResolve</span>(<span
                    class="cm-variable">readOrdinaryObject</span>(<span class="cm-variable">unshared</span>));<span
                    class="cm-comment">//重点查看readOrdinaryObject方法</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation"
                                                                                                                                               cm-text="	">    </span><span class="cm-tab"
                                                                                                                                                                               role="presentation"
                                                                                                                                                                               cm-text="	">    </span>...</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  } <span class="cm-keyword">finally</span> {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">depth</span><span
                    class="cm-operator">--</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">bin</span>.<span class="cm-variable">setBlockDataMode</span>(<span
                    class="cm-variable">oldMode</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  } &nbsp; &nbsp;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">24</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                             style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">private</span> <span class="cm-variable-3">Object</span> <span class="cm-variable">readOrdinaryObject</span>(<span
                    class="cm-variable-3">boolean</span> <span class="cm-variable">unshared</span>) <span class="cm-keyword">throws</span> <span
                    class="cm-variable">IOException</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">26</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span>...</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">27</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">//isInstantiable 返回true，执行 desc.newInstance()，通过反射创建新的单例类，</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">28</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">obj</span> <span class="cm-operator">=</span> <span
                    class="cm-variable">desc</span>.<span class="cm-variable">isInstantiable</span>() <span class="cm-operator">?</span> <span class="cm-variable">desc</span>.<span
                    class="cm-variable">newInstance</span>() : <span class="cm-atom">null</span>; </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                           style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  ...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                           style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">30</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span
                    class="cm-comment">// 在Singleton类中添加 readResolve 方法后 desc.hasReadResolveMethod() 方法执行结果为true</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">31</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span
                    class="cm-variable">obj</span> <span class="cm-operator">!=</span> <span class="cm-atom">null</span> <span class="cm-operator">&amp;&amp;</span> <span
                    class="cm-variable">handles</span>.<span class="cm-variable">lookupException</span>(<span class="cm-variable">passHandle</span>) <span class="cm-operator">==</span> <span
                    class="cm-atom">null</span> <span class="cm-operator">&amp;&amp;</span> <span class="cm-variable">desc</span>.<span
                    class="cm-variable">hasReadResolveMethod</span>()) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">32</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  <span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">// 通过反射调用 Singleton 类中的 readResolve 方法，将返回值赋值给rep变量</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">33</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  <span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">// 这样多次调用ObjectInputStream类中的readObject方法，继而就会调用我们定义的readResolve方法，所以返回的是同一个对象。</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">34</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  <span class="cm-tab" role="presentation" cm-text="	">    </span><span
                    class="cm-variable-3">Object</span> <span class="cm-variable">rep</span> <span class="cm-operator">=</span> <span class="cm-variable">desc</span>.<span
                    class="cm-variable">invokeReadResolve</span>(<span class="cm-variable">obj</span>);</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">35</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-tab" role="presentation"
                                                                                                                                               cm-text="	">   </span>...</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">36</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">37</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">obj</span>;</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">38</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 884px;"></div><div class="CodeMirror-gutters" style="height: 884px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
        </li>
        <li><p><span>反射方式破解单例的解决方法</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>30</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Singleton</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//私有构造方法</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span
                    class="cm-variable">Singleton</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/*</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">6</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">反射破解单例模式需要添加的代码</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">8</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span
                    class="cm-variable">instance</span> <span class="cm-operator">!=</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">throw</span> <span
                    class="cm-keyword">new</span> <span class="cm-variable">RuntimeException</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                             style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-keyword">volatile</span> <span
                    class="cm-variable">Singleton</span> <span class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                            style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">//对外提供静态方法获取该对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable">Singleton</span> <span
                    class="cm-variable">getInstance</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span class="cm-variable">instance</span> <span
                    class="cm-operator">!=</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                               style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">20</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">synchronized</span> (<span
                    class="cm-variable">Singleton</span>.<span class="cm-keyword">class</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                          style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span>(<span
                    class="cm-variable">instance</span> <span class="cm-operator">!=</span> <span class="cm-atom">null</span>) {</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">24</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-keyword">return</span> <span class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">26</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">instance</span> <span
                    class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Singleton</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">27</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">instance</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">28</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                       style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">30</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 680px;"></div><div class="CodeMirror-gutters" style="height: 680px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><font color="red"><span>说明:</span></font></p>
            <p><span>	</span><span>这种方式比较好理解。当通过反射方式调用构造方法进行创建创建时，直接抛异常。不运行此中操作。</span></p></li>
    </ul>
    <p>&nbsp;</p>
    <h3><a name="414-jdk源码解析-runtime类" class="md-header-anchor"></a><span>4.1.4 JDK源码解析-Runtime类</span></h3>
    <p><span>Runtime类就是使用的单例设计模式。</span></p>
    <ol start=''>
        <li><p><span>通过源代码查看使用的是哪儿种单例模式</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>19</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">Runtime</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-keyword">static</span> <span class="cm-variable">Runtime</span> <span
                    class="cm-variable">currentRuntime</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">Runtime</span>();</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">* Returns the runtime object associated with the current Java application.</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span
                    class="cm-comment">* Most of the methods of class &lt;code&gt;Runtime&lt;/code&gt; are instance</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">7</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">* methods and must be invoked with respect to the current runtime object.</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">*</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">9</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">* @return  the &lt;code&gt;Runtime&lt;/code&gt; object associated with the current</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">* &nbsp; &nbsp; &nbsp; &nbsp;  Java application.</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; <span class="cm-comment">*/</span></span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span
                    class="cm-keyword">static</span> <span class="cm-variable">Runtime</span> <span class="cm-variable">getRuntime</span>() {</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span
                    class="cm-variable">currentRuntime</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                         style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/** Don't let anyone else instantiate this class */</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">private</span> <span class="cm-variable">Runtime</span>() {}</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">18</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp;  ...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                           style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 431px;"></div><div class="CodeMirror-gutters" style="height: 431px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p><span>从上面源代码中可以看出Runtime类使用的是恶汉式（静态属性）方式来实现单例模式的。</span></p>
            <p>&nbsp;</p></li>
        <li><p><span>使用Runtime类中的方法</span></p>
            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" style="break-inside: unset;"><div
                    class="CodeMirror cm-s-inner CodeMirror-wrap" lang="java"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 44px;"><textarea
                    autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"
                    style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler"
                                                                                                                                             cm-not-content="true"></div><div
                    class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer"
                                                                                                                                   style="margin-left: 36px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div
                    style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div
                    class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>19</div></div></div><div
                    class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div
                    class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div
                    class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -36px; width: 36px;"></div><div
                    class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                                             style="left: 0px; width: 27px;">1</div></div><pre class=" CodeMirror-line "
                                                                                                                                                               role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span
                    class="cm-def">RuntimeDemo</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">public</span> <span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span
                    class="cm-variable">main</span>(<span class="cm-variable-3">String</span>[] <span class="cm-variable">args</span>) <span class="cm-keyword">throws</span> <span
                    class="cm-variable">IOException</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//获取Runtime类对象</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">4</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Runtime</span> <span class="cm-variable">runtime</span> <span
                    class="cm-operator">=</span> <span class="cm-variable">Runtime</span>.<span class="cm-variable">getRuntime</span>();</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">5</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">6</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//返回 Java 虚拟机中的内存总量。</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">7</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span
                    class="cm-variable">println</span>(<span class="cm-variable">runtime</span>.<span class="cm-variable">totalMemory</span>());</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">8</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//返回 Java 虚拟机试图使用的最大内存量。</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">9</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                   role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span
                    class="cm-variable">println</span>(<span class="cm-variable">runtime</span>.<span class="cm-variable">maxMemory</span>());</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 27px;">10</div></div><pre class=" CodeMirror-line "
                                                                                                                                                      role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                        style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
                    class="cm-comment">//创建一个新的进程执行指定的字符串命令，返回进程对象</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper"
                                                                                                                                                          style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">Process</span> <span class="cm-variable">process</span> <span
                    class="cm-operator">=</span> <span class="cm-variable">runtime</span>.<span class="cm-variable">exec</span>(<span class="cm-string">"ipconfig"</span>);</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">13</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">//获取命令执行后的结果，通过输入流获取</span></span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">14</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">InputStream</span> <span
                    class="cm-variable">inputStream</span> <span class="cm-operator">=</span> <span class="cm-variable">process</span>.<span
                    class="cm-variable">getInputStream</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div
                    class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable-3">byte</span>[] <span class="cm-variable">arr</span> <span
                    class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable-3">byte</span>[<span class="cm-number">1024</span> <span
                    class="cm-operator">*</span> <span class="cm-number">1024</span><span class="cm-operator">*</span> <span class="cm-number">100</span>];</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">16</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">b</span> <span
                    class="cm-operator">=</span> <span class="cm-variable">inputStream</span>.<span class="cm-variable">read</span>(<span class="cm-variable">arr</span>);</span></pre></div><div
                    style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt"
                                                                                                                 style="left: 0px; width: 27px;">17</div></div><pre class=" CodeMirror-line "
                                                                                                                                                                    role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span
                    class="cm-variable">println</span>(<span class="cm-keyword">new</span> <span class="cm-variable-3">String</span>(<span class="cm-variable">arr</span>,<span
                    class="cm-number">0</span>,<span class="cm-variable">b</span>,<span class="cm-string">"gbk"</span>));</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 27px;">18</div></div><pre
                    class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div><div style="position: relative;"><div
                    class="CodeMirror-gutter-wrapper" style="left: -36px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show"
                                                                                style="left: 0px; width: 27px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span
                    role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div
                    style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 431px;"></div><div class="CodeMirror-gutters" style="height: 431px;"><div
                    class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 35px;"></div></div></div></div></pre>
            <p>&nbsp;</p></li>
    </ol>
</div>
</body>
</html>